package com.itboye.sunsun.luntan.fragment;

import com.itboye.sunsun.R;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.OrderBean;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.custome.XListView;
import com.itboye.sunsun.custome.XListView.IXListViewListener;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.support.BaseFragment;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public abstract class AbstractTieZiFragment extends BaseFragment {

	private XListView xlistview;
	private int pageNum = 1;
 
	private BaseAdapter adapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_wo_de_tiezi, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		xlistview.setPullRefreshEnable(true);
		xlistview.setPullLoadEnable(true);
		xlistview.setXListViewListener(new IXListViewListener() {

			@Override
			public void onRefresh() {
				pullDown();
			}

			@Override
			public void onLoadMore() {
				pullUp();
			}

		});
		// adapter = new ShopCarAdapter(getApplication(), dataList, this);
		// xlistview.setAdapter(adapter);
		adapter=onGetAdapter();
		xlistview.setAdapter(adapter);
		pullDown();

	}


	protected abstract BaseAdapter onGetAdapter();
	
	protected abstract String onGetTypeKey() ;
	
	protected abstract void onPullDownFinish(BaseAdapter adapter,XListView xListView,OrderBean orderBean);

	protected abstract void onPullUpFinish(BaseAdapter adapter,XListView xListView,OrderBean orderBean);
	
	protected abstract void onPullDownError(BaseAdapter adapter,XListView xListView,Exception exception, int code, String msg);

	protected abstract void onPullUpError(BaseAdapter adapter,XListView xListView,Exception exception, int code, String msg);
	

	private void pullDown() {

		pageNum = 1;
		MyJsonRequest.Builder<OrderBean> builder = new MyJsonRequest.Builder<OrderBean>()
				.param("uid",(String)SPUtils.get(getContext(), null, SPContants.USER_ID	, "")).apiVer("100")
				.param("page_no", "" + pageNum) 
				.typeKey(onGetTypeKey())
				.requestListener(new XRequestListener<OrderBean>() {

					@Override
					public void onResponse(OrderBean orderBean) {
						onPullDownFinish(adapter, xlistview, orderBean);
						pageNum++;
						xlistview.stopRefresh();

					}
				}).errorListener(new XErrorListener() {

					@Override
					
					public void onErrorResponse(Exception exception, int code, String msg) {
						xlistview.stopRefresh();
						onPullDownError(adapter, xlistview, exception, code, msg);
					}
				});

		MyJsonRequest<OrderBean> request = builder.build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

	private void pullUp() {
		MyJsonRequest.Builder<OrderBean> builder = new MyJsonRequest.Builder<OrderBean>()
				.param("uid", (String) SPUtils.get(App.ctx, null, SPContants.USER_ID, "")).apiVer("100")
				.typeKey(onGetTypeKey())
				.param("page_no", "" + pageNum) 

				.requestListener(new XRequestListener<OrderBean>() {

					@Override
					public void onResponse(OrderBean orderBean) {
						onPullUpFinish(adapter, xlistview, orderBean);
						pageNum++;
						xlistview.stopLoadMore();
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						xlistview.stopLoadMore();
						onPullUpError(adapter, xlistview, exception, code, msg);
					}
				});

		MyJsonRequest<OrderBean> request = builder.build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

	
	
}
